package com.dfrz.problem.service.impl;

import java.util.List;

import com.dfrz.problem.dao.SysAdminDao;
import com.dfrz.problem.model.SysAdmin;
import com.dfrz.problem.service.SysAdminService;
import com.dfrz.problem.utils.IConstant;
import com.dfrz.problem.utils.MD5Util;
import com.dfrz.problem.utils.Result;

public class SysAdminServiceImpl implements SysAdminService {
	private SysAdminDao sysAdminDao;
	
	@Override
	public boolean updatePwd(String email, String pwd) {
		return sysAdminDao.updatePwd(email,MD5Util.getMD5Str(pwd));
	}

	public void setSysAdminDao(SysAdminDao sysAdminDao) {
		this.sysAdminDao = sysAdminDao;
	}

	@Override
	public boolean checkPwd(String password, String email) {
		return sysAdminDao.checkPwd(MD5Util.getMD5Str(password),email);
	}

	@Override
	public Result<SysAdmin> addSysAdmin(SysAdmin admin) {
		Result<SysAdmin> result = new Result<SysAdmin>();
		// 检查该管理员是否已存在
		SysAdmin sysAdmin = sysAdminDao.getSysAdmin(admin.getAdminEmail());
		if(sysAdmin != null){
			result.setCode(IConstant.SYSTEMADMIN_ALREADY_EXIST);
			return result;
		}
		
		List<SysAdmin> list = sysAdminDao.getAllAdmin();
		if(list.size() >= 3){
			result.setCode(IConstant.SYSTEMADMIN_MAXIMUM_3);
			return result;
		}
		
		admin.setPassword(MD5Util.getMD5Str(admin.getPassword()));
		sysAdminDao.addSysAdmin(admin);
		
		return result;
	}
}
